package com.example.system.controller;

import com.example.common.util.Result;
import com.example.common.util.ResultCodeEnum;
import com.example.system.SysUser;
import com.example.system.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "用户管理")
@RestController
@RequestMapping("admin/system/sysUser")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @ApiOperation(value = "分页查询列表")
    @GetMapping("{page}/{limit}")
//    public Result getPageList(@PathVariable("page") int page,
//                              @PathVariable("limit") int limit,
//                              SearchDto sysUser)  //GET不能用@ResquestBody
    public Result getPageList(@PathVariable("page") int page,
                              @PathVariable("limit") int limit,
                              @RequestParam(value = "keyword", required = false) String keyword,
                              @RequestParam(value = "createTimeBegin", required = false) String createTimeBegin,
                              @RequestParam(value = "createTimeEnd", required = false) String createTimeEnd){
        return sysUserService.getPageList(page,limit,keyword,createTimeBegin,createTimeEnd);
    }

//    http://localhost:9528/dev-api/user/admin/system/sysUser/get/12 GET
    @ApiOperation(value = "根据id获取用户")
    @GetMapping("get/{id}")
    public Result getById(@PathVariable("id") Long id){
        if(id == null || id == 0l){
            return Result.fail("id异常");
        }
        SysUser sysUser = sysUserService.getById(id);
        return Result.ok(sysUser);
    }

//    http://localhost:9528/dev-api/user/admin/system/sysUser/save POST
    @ApiOperation(value = "新增用户信息")
    @PostMapping("save")
    public Result save(@RequestBody SysUser sysUser){
        if(sysUser.equals(null)){
            return Result.build("添加失败",ResultCodeEnum.DATA_ERROR);
        }
        boolean save = sysUserService.save(sysUser);
        return Result.ok(save);
    }

//    http://localhost:9528/dev-api/user/admin/system/sysUser/update PUT
    @ApiOperation(value = "更新用户信息")
    @PutMapping("update")
    public Result update(@RequestBody SysUser sysUser){
        if(sysUser.equals(null)){
            return Result.build("修改失败",ResultCodeEnum.DATA_ERROR);
        }
        boolean b = sysUserService.updateById(sysUser);
        return Result.ok(b);
    }

//    http://localhost:9528/dev-api/user/admin/system/sysUser/remove/4 DELETE
    @ApiOperation(value = "根据id删除用户信息")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable("id") Long id){
        if(id == null || id == 0l){
            return Result.fail("id异常");
        }
        boolean b = sysUserService.removeById(id);
        return Result.ok(b);
    }

//    http://localhost:9528/dev-api/user/admin/system/sysUser/updateStatus/1/0 GET
    @ApiOperation(value = "修改用户状态")
    @GetMapping("updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable("id") Long id,
                               @PathVariable("status") Integer status){
        if(id == null && status == null){
            return Result.fail();
        }
        SysUser sysUser = new SysUser();
        sysUser.setId(id);
        sysUser.setStatus(status);
        boolean update = sysUserService.updateById(sysUser);
        return Result.ok(update);
    }

    @ApiOperation(value = "获取当前用户基本信息")
    @GetMapping("getCurrentUser")
    public Result getCurrentUser(){
        return Result.ok(sysUserService.getCurrentUser());
    }


}
